package me.leiusual.sharing.ui.store

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import androidx.paging.PagedList
import me.leiusual.sharing.api.model.storeproductquery.Product
import me.leiusual.sharing.pref.Store
import me.leiusual.sharing.repository.StoreRepository
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-25
 * description:
 */
class StoreAreaViewModel @Inject constructor(
    private val storeRepository: StoreRepository,
    val store: Store
) : ViewModel() {

  // 商品查询

  private val _storeProductQuery = MutableLiveData<ParamsStoreProductQuery>()
  val storeProductQuery: LiveData<PagedList<Product>> = switchMap(_storeProductQuery) {
    storeRepository.storeProductQuery(it.storeId, it.storeAreaId, it.categoryId)
  }

  fun storeProductQuery(storeId: Long, storeAreaId: Long, categoryId: Long) {
    _storeProductQuery.value = ParamsStoreProductQuery(storeId, storeAreaId, categoryId)
  }

  data class ParamsStoreProductQuery(val storeId: Long, val storeAreaId: Long, val categoryId: Long)
}